package helper

import (
	"gitee.com/yangguoliang001/go-gin-vue3/QO"
	"gitee.com/yangguoliang001/go-gin-vue3/pkg/response"
	"gitee.com/yangguoliang001/go-gin-vue3/util/code"
	"github.com/duke-git/lancet/v2/convertor"
	"github.com/gin-gonic/gin"
)

// SearchCache 查询缓存
// @Summary 查询缓存
// @Description 查询缓存
// @Tags API.Helper
// @Accept application/x-www-form-urlencoded
// @Produce json
// @Param redisKey formData string true "Redis Key"
// @Success 200 {object} response.TResponse{data=vo.SearchCacheVO}
// @Failure 400 {object}  response.TResponse{data=interface{}}
// @Router /api/helper/cache/search [post]
// @Security LoginToken
func (h helperController) SearchCache(c *gin.Context) {
	var cacheReq QO.CacheReq
	if err := c.ShouldBind(&cacheReq); err != nil {
		response.Fail(c, code.ParamBindError, code.Text(code.ParamBindError), gin.H{"realErr": err})
		return
	}
	cacheRes, err := h.helperService.SearchCache(c, cacheReq)
	cacheMap, _ := convertor.StructToMap(cacheRes)
	if err == nil {
		// 结构体 -> map
		response.Success(c, "查询缓存数据成功", cacheMap)
	}
}

// ClearCache 清空缓存
// @Summary 清空缓存
// @Description 清空缓存
// @Tags API.Helper
// @Accept application/x-www-form-urlencoded
// @Produce json
// @Param redisKey formData string true "Redis Key"
// @Success 200 {object} response.TResponse{data=vo.ClearCacheVO}
// @Failure 400 {object}  response.TResponse{data=interface{}}
// @Router /api/helper/cache/clear [patch]
// @Security LoginToken
func (h helperController) ClearCache(c *gin.Context) {
	var cacheReq QO.CacheReq
	if err := c.ShouldBind(&cacheReq); err != nil {
		response.Fail(c, code.ParamBindError, code.Text(code.ParamBindError), gin.H{"realErr": err})
		return
	}

	cache, err := h.helperService.ClearCache(c, cacheReq)
	cacheMap, _ := convertor.StructToMap(cache)
	if err == nil {
		response.Success(c, "清空缓存数据成功", cacheMap)
	}
}
